package com.meituan.catering.management.shop.biz.service.impl;


import com.google.common.collect.ImmutableSet;
import com.meituan.catering.management.shop.api.http.model.request.SearchShopHttpRequest;
import com.meituan.catering.management.shop.biz.model.ShopBO;
import com.meituan.catering.management.shop.biz.model.common.PageBO;
import com.meituan.catering.management.shop.biz.model.common.UserContextBO;
import com.meituan.catering.management.shop.biz.model.converter.ShopBOConverter;
import com.meituan.catering.management.shop.biz.model.converter.ShopDOConverter;
import com.meituan.catering.management.shop.biz.service.ShopBizQueryService;
import com.meituan.catering.management.shop.dao.mapper.ShopMapper;
import com.meituan.catering.management.shop.dao.model.ShopDO;
import com.meituan.catering.management.shop.dao.model.request.SearchShopDataRequest;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import static com.meituan.catering.management.shop.biz.model.converter.ShopBOConverter.buildPageBO;
import static org.apache.commons.lang3.ObjectUtils.isNotEmpty;


/**
 * @Author:ale
 * @Date 2022/4/23 16:32
 * @Version 1.0
 */
@Service
public class ShopBizQueryServiceImpl implements ShopBizQueryService {
    @Resource
    private ShopMapper shopMapper;

    @Override
    public PageBO<ShopBO> searchForPage(SearchShopHttpRequest request) {
        int totalCount = shopMapper.countForPageOne();
        PageBO<ShopBO> shopPageBO = buildPageBO(request, totalCount);
        if (totalCount > 0) {
            List<ShopDO> shopDOList = shopMapper.allList(request);
            shopPageBO.getRecords().addAll(shopDOList.stream().map(ShopBOConverter::fromDO).collect(Collectors.toList()));
        }
        return shopPageBO;
    }
    @Override
    public PageBO<ShopBO> queryForPage(SearchShopHttpRequest request) {
        SearchShopDataRequest dataRequest = ShopDOConverter.buildDataRequest(request);
        int totalCount = shopMapper.countForPage(dataRequest);
        PageBO<ShopBO> shopPageBO = buildPageBO(request,totalCount);
        if (totalCount > 0){
            List<ShopDO> shopDOs =shopMapper.searchForPage(dataRequest);
            shopPageBO.getRecords().addAll(shopDOs.stream().map(ShopBOConverter::fromDO).collect(Collectors.toList()));
            if (shopPageBO.getRecords() == null){
                List<ShopDO> shopDOList = shopMapper.allList(request);
                shopPageBO.getRecords().addAll(shopDOList.stream().map(ShopBOConverter::fromDO).collect(Collectors.toList()));
            }
        }else {
            List<ShopDO> shopDOList = shopMapper.allList(request);
            shopPageBO.getRecords().addAll(shopDOList.stream().map(ShopBOConverter::fromDO).collect(Collectors.toList()));
        }
        return shopPageBO;
    }

    @Override
    public PageBO<ShopBO> searchForPageTwo(UserContextBO userContext,SearchShopHttpRequest request) {
        SearchShopDataRequest dataRequest = ShopDOConverter.fromRequest(userContext,request,findPrivilegedSHopIds(userContext));
        int totalCount = shopMapper.countForPage(dataRequest);
        PageBO<ShopBO> shopPageBO = buildPageBO(request,totalCount);
        if (totalCount > 0){
            List<ShopDO> shopDOs =shopMapper.searchForPage(dataRequest);
            shopPageBO.getRecords().addAll(shopDOs.stream().map(ShopBOConverter::fromDO).collect(Collectors.toList()));
            if (shopPageBO.getRecords() == null){
                List<ShopDO> shopDOList = shopMapper.allList(request);
                shopPageBO.getRecords().addAll(shopDOList.stream().map(ShopBOConverter::fromDO).collect(Collectors.toList()));
            }
        }else {
            List<ShopDO> shopDOList = shopMapper.allList(request);
            shopPageBO.getRecords().addAll(shopDOList.stream().map(ShopBOConverter::fromDO).collect(Collectors.toList()));
        }
        return shopPageBO;
    }

    @Override
    public Set<Long> findPrivilegedSHopIds(UserContextBO userContext) {
        Set<Long> privilegedShopIds = new LinkedHashSet<>();
        Set<Long> subordinateIds = ImmutableSet.of(userContext.getShopId());
        while (isNotEmpty(subordinateIds)){
            privilegedShopIds.addAll(subordinateIds);
            subordinateIds = shopMapper.findSubordinateIds(userContext.getTenantId());
        }
        return privilegedShopIds;
    }


}
